﻿using Currency.Infra.Entities;
using Currency.Infra.EntityFramework;
using Currency.Infra.IRepository;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace Currency.Infra.Repository
{
    /// <summary>
    /// 仓储
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    internal partial class Repository<TEntity> : IRepository<TEntity> where TEntity : Entity
    {
        protected virtual EfCoreDbContext DbContext { get; }

        protected virtual DbSet<TEntity> DbSet { get; }

        public Repository(EfCoreDbContext dbContext)
        {
            DbContext = dbContext;
            DbSet = dbContext.Set<TEntity>();
        }

        protected virtual IQueryable<TEntity> GetDbSet(bool writeDb, bool noTracking)
        {
            if (noTracking && writeDb)
                return DbContext.Set<TEntity>().AsNoTracking().TagWith("");
            else if (noTracking)
                return DbContext.Set<TEntity>().AsNoTracking();
            else if (writeDb)
                return DbContext.Set<TEntity>().TagWith("");
            else
                return DbContext.Set<TEntity>();
        }
    }
}
